<template>
	<view class="padding-30">
		<view class="padding-30 bg-fff border-radius20 margin-bottom30">
			<view class="flex-between padding-bottom30 f-divider1 margin-bottom30">
				<view class="fontSize-15 color-000 margin-right20">报告名称</view>
				<input type="text" v-model="check_name" placeholder="请输入报告名称" class="input-item" />
			</view>
			<view class="flex-between padding-bottom30 f-divider1 margin-bottom30">
				<view class="fontSize-15 color-000 margin-right20">体检时间</view>
				<view class="flex-alignItems-center" @click="tijianDateShow = true">
					<view class="fontSize-15 color-000 margin-right10" v-if="tijianDateName">{{tijianDateName}}</view>
					<view class="fontSize-15 color-999 margin-right10" v-else>请选择体检时间</view>
					<u-icon name="arrow-right" color="#666666" size="18"></u-icon>
				</view>
			</view>
		</view>
		<view class="padding-30 bg-fff border-radius20">
			<view class="margin-bottom30">
				<text class="fontSize-15 color-000">上传报告</text>
				<text class="fontSize-15 color-999">（图片最多可上传10张）</text>
			</view>
			<view>
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="10" :previewImage="true"></u-upload>
			</view>
		</view>

		<!-- 底部操作栏 -->
		<view style="height: 200rpx;"></view>
		<view class="position-fixed-bottom padding-30 bg-fff">
			<view class="btn50-0165FD-15ffffff flex-center" @click="SubmitEvent">
				保存
			</view>
		</view>

		<!-- 体检时间 -->
		<u-datetime-picker :maxDate="Number(new Date())" :show="tijianDateShow" v-model="tijianDateValue" mode="date"
			@cancel="tijianDateCancel" @confirm="tijianDateConfirm"></u-datetime-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				check_name: '', // 体检报告名称
				tijianDateShow: false,
				tijianDateValue: Number(new Date()),
				tijianDateName: '',
				fileList1: [],
				images:'',
				id:''
			}
		},
		onLoad(e) {
			if(e.id){
				if(uni.getStorageSync('patlient-item')){
					this.sick_id = uni.getStorageSync('patlient-item').id
				}
				this.id = e.id
				this.getDetail()
			}
			if(uni.getStorageSync('patlient-item')){
				this.sick_id = uni.getStorageSync('patlient-item').id
			}
		},
		methods: {
			// 获取详情
			async getDetail(){
				let res = await this.$http.post(this.$api.tijian_detail, {
					id:this.id,
					sick_id: this.sick_id
				}, {
					token: true
				})
				if (res.code == 1) {
					this.live_detail = res.data
					this.check_name = res.data.check_name
					this.tijianDateName = res.data.check_date
					this.fileList1 = res.data.images.map(item=>{
						return {
							status: 'success',
							message: '',
							url: item
						}
					})
				}
			},
			// 提交
			async SubmitEvent(){
				if(!this.check_name)return this.$tools.toast('请填写报告名称')
				if(!this.tijianDateName)return this.$tools.toast('请选择体检时间')
				let form = {
					sick_id:this.sick_id,
					check_name:this.check_name,
					check_date:this.tijianDateName,
					images:''
				}
			    this.fileList1.forEach(item => {
			    	form.images += item.url + ','
			    })
			    form.images = form.images.slice(0, -1);
				if(this.id){
					form.id = this.id
					let res = await this.$http.post(this.$api.tijian_edit,form,{token:true})
					if(res.code ==1){
						this.$tools.toast('修改成功')
						this.$tools.navigateBack(1,200)
					}
				}else{
					let res = await this.$http.post(this.$api.tijian_add,form,{token:true})
					if(res.code ==1){
						this.$tools.toast('添加成功')
						this.$tools.navigateBack(1,200)
					}
				}
			},
			// 上传图片
			deletePic(event) { // 删除图片
				this.fileList1.splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileList1.length
				lists.map((item) => {
					this.fileList1.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.fileList1[fileListLen]
					this.fileList1.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: this.$api.upload + 'common/upload', // 仅为示例，非真实的接口地址
						method: 'POST',
						filePath: url,
						name: 'file',
						formData: {
							token: uni.getStorageSync('token')
						},
						success: (res) => {
							console.log(res)
							let data = JSON.parse(res.data)
							resolve(data.data.url)
						}
					});
				})
			},
			// 确认体检时间
			tijianDateConfirm(e) {
				this.tijianDateName = this.$T.timeFormat(e.value).substr(0, 10)
				this.tijianDateShow = false
			},
			// 取消体检时间
			tijianDateCancel() {
				this.tijianDateName = ''
				this.tijianDateShow = false
			},
		}
	}
</script>

<style lang="scss" scoped>

</style>